//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssLossOrderDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssLossOrder;
import com.aidong.model.wills.PssLossOrderList;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"productGainsLosses"})
public class ProductGainsLossesController extends PopProductController {
    @Resource
    PssLossOrderDao pssLossOrderDao;
    @Resource
    PssProductStockDao pssProductStockDao;

    public ProductGainsLossesController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pss/productgainslosses/list")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "baosunyiDateFrom",required = false) String baosunyiDateFrom, @RequestParam(value = "baosunyiDateTo",required = false) String baosunyiDateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "LossShop", shopCode);
        if(!this.chkNull(baosunyiDateFrom)) {
            cd.ge("LossDate", this.parseDate(baosunyiDateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(baosunyiDateTo)) {
            cd.le("LossDate", this.parseDate(baosunyiDateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.pssLossOrderDao.getLstLossOrder(pg, cd);
        ModelAndView mv = new ModelAndView("pss/productgainslosses/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("ProductcodeList", this.getTypeList("ProductcodeList"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("shopCode") String shopCode) {
        PssLossOrder pssLossOrder = new PssLossOrder();
        pssLossOrder.setLossShop(shopCode);
        ModelAndView mv = new ModelAndView("pss/productgainslosses/edit");
        mv.addObject("pssLossOrder", pssLossOrder);
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("productUnit", this.getTypeList("productUnit"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssLossOrder") PssLossOrder pssLossOrder, @RequestParam(value = "lossShop",required = false) String lossShop, @RequestParam(value = "note",required = false) String note, Products products) {
        if(this.chkNull(lossShop)) {
            return this.getResult(false, "请选择门店");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请选择要报损的商品");
        } else {
            try {
                Date e = new Date();
                pssLossOrder.setLossDate(e);
                pssLossOrder.setCreateUser(this.getLoginUserCode());
                pssLossOrder.setCreateTime(e);
                pssLossOrder.setUpdateTime(e);
                PssLossOrderList pss = null;
                ArrayList lst = new ArrayList();
                Iterator i$ = products.getLstProduct().iterator();

                while(i$.hasNext()) {
                    PssPopProduct pc = (PssPopProduct)i$.next();
                    pss = new PssLossOrderList();
                    if(!this.chkNull(pc.getProductCode())) {
                        pss.setProductCode(pc.getProductCode());
                        pss.setQuantity(pc.getQty());
                        pss.setCreateUser(this.getLoginUserCode());
                        pss.setCreateTime(e);
                        pss.setUpdateTime(e);
                        lst.add(pss);
                    }
                }

                this.pssLossOrderDao.addLossGainProduct(pssLossOrder, lst, this.pssProductStockDao);
                this.addUserLog("报损溢", "损溢单号:" + pssLossOrder.getLossId());
                return this.getResult(true, "保存完成");
            } catch (Exception var10) {
                this.log.error("add pssLossOrder", var10);
                return this.getResult(false, "报损溢发生错误");
            }
        }
    }
}
